'use strict';

app.controller('shopSetCtrl', function ($scope,$http,$rootScope,FileUploader) {
    // console.log("dddddddddd");
    // 自己写的
    //=======uploaderImage相关 Start======//
    // $scope.uploadStatus = false; //定义上传后返回的状态，成功获失败
    var uploaderImage = $scope.uploaderImage = new FileUploader({
        url: '/api/imgUploadMeng',
        method: 'POST',
        // queueLimit: 1,     //文件个数
        removeAfterUpload: true   //上传后删除文件
        // formData:[
        //     {
        //         loginId: "123"
        //     }
        // ]
    });

    
    $scope.setImg = "";//显示上传文件名称
    $scope.file_data = [];
    $scope.shopData = $rootScope.shop_set.img == "" ? "" : $rootScope.shop_set;
    console.log($scope.shopData);
    $scope.setImg = $scope.shopData.img == '' ? uploaderImage.queue[0].file.name : $rootScope.shop_set.img;

    // 图片过滤器
    uploaderImage.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });

    // 上传图片后重新定义数组，以便数组中只有一个元素
    $scope.uploaderImage.onAfterAddingFile = function (fileItem) {
        while (this.queue.length > 1) {
            this.queue.shift();
        }
        $scope.setImg = uploaderImage.queue[0].file.name;//赋值给setImg用于前端显示
        $scope.file_data = fileItem;
        // console.log(fileItem);//父
        // console.log(uploaderImage);//子
        //能够在这里判断添加的文件名后缀和文件大小是否满足需求。
        console.log("onAfterAddingFile");
    };
    
    $scope.checkImg = function(){    //重新选择文件时，清空队列，达到覆盖文件的效果
        
        uploaderImage.clearQueue();

    };

    uploaderImage.onSuccessItem = function(fileItem, response, status, headers) {
        // $scope.uploadStatus = true;   //上传成功则把状态改为true
        console.log(response);
        console.log("onSuccessItem");
        if(response == 'Files were uploaded.'){
            $http.post('/api/saveShopdata',$scope.shopData)
            .then(function(response){
                if(response.status == 200){
                    console.log(response);
                }
                else{
                    console.log(response.status);
                    console.log(response.data);
                }
            });
        }
        else{
            console.log(response.status);
            console.log(response.data);
        }

    };
    uploaderImage.onCompleteItem = function(fileItem, response, status, headers) {
        console.log("onCompleteItem");
    };
    uploaderImage.onErrorItem = function(fileItem, response, status, headers) {
        console.log("onErrorItem");
    };


    // 提交保存
    $scope.submitShopData = function(){
        // console.log($scope.shopData);
        // console.log($scope.uploaderImage);
        // debugger;
        //没有上传或更改图片
        if (uploaderImage.queue.length == 0) {

            if ($scope.shopData != "") {

                console.log($scope.shopData);
                $http.post('/api/saveShopdata',$scope.shopData)
                .then(function(response){
                    if(response.status == 200){
                        console.log(response);
                    }
                    else{
                        console.log(response.status);
                        console.log(response.data);
                    }
                });

            }else{

                alert('无数据！');
            }

        }
        //上传图片或更改图片
        else{
            
            uploaderImage.uploadAll();
            // console.log($scope.uploaderImage);
            // if( $scope.uploadStatus){
                
            //     alert('上传成功！');
               
            // }else{
            //     alert('上传失败！');
               
            // }
        }


        

    };



    // $scope.uploadStatus = false; //定义两个上传后返回的状态，成功获失败
    // var uploader = $scope.uploader = new FileUploader({
    //     url: '/api/imgUpload',
    //     method: 'POST',
    //     queueLimit: 1,     //文件个数 
    //     removeAfterUpload: true   //上传后删除文件
    // });
    // $scope.clearItems = function(){    //重新选择文件时，清空队列，达到覆盖文件的效果
    //     uploader.clearQueue();
    // }
    // uploader.onAfterAddingFile = function(fileItem) {
    //     $scope.fileItem = fileItem._file;    //添加文件之后，把文件信息赋给scope,能够在这里判断添加的文件名后缀和文件大小是否满足需求
    // };
    // uploader.onSuccessItem = function(fileItem, response, status, headers) {
    //     debugger;
    //     console.log("aaaa");
    //     $scope.uploadStatus = true;   //上传成功则把状态改为true
    // };
    // uploader.onErrorItem = function(fileItem, response, status, headers) {
    //     console.info(response);
    // };

    // $scope.UploadFile = function(){
    //     uploader.uploadAll();

    //     if($scope.uploadStatus){
    //         debugger;
    //         alert('上传成功！');
           
    //     }else{
    //         debugger;
    //         alert('上传失败！');
           
    //     }
    // }






    
});